﻿using SharpDX.Direct3D;
using SharpDX.Direct3D11;
using SharpDX.DXGI;
using SharpDX.Windows;
using System.Drawing;
using System.IO;
using System.Reflection;
using System.Windows.Forms;
using Device = SharpDX.Direct3D11.Device;

namespace MikadoShooter
{
    public static partial class GameHelper
    {
        public static DXWindow WindowInit()
        {
            Device device;
            SwapChain swapChain;
            RenderForm form = new RenderForm(Application.ProductName)
            {
                Height = Screen.PrimaryScreen.WorkingArea.Height,
                Width = (int)(Screen.PrimaryScreen.WorkingArea.Height / 1.66667f),
                MaximizeBox = false,
                StartPosition = FormStartPosition.CenterScreen
            };

            //Set icon
            using (Stream stream = Assembly.GetExecutingAssembly().GetManifestResourceStream("MikadoShooter.Context.icon.ico"))
            {
                if (stream != null)
                    form.Icon = new Icon(stream);
            }
 
            //Note DeviceCreationFlags.Debug REQUIRE DirectX SDK!!
            Device.CreateWithSwapChain(DriverType.Hardware, DeviceCreationFlags.None, new SwapChainDescription
            {
                BufferCount = 1,
                ModeDescription = new ModeDescription(form.ClientSize.Width,form.ClientSize.Height,new Rational(60,1),Format.R8G8B8A8_UNorm),
                IsWindowed = true,
                OutputHandle = form.Handle,
                SampleDescription = new SampleDescription(1, 0),
                SwapEffect = SwapEffect.Discard,
                Usage = Usage.RenderTargetOutput
            }, out device,out swapChain);

            using (Factory factory = swapChain.GetParent<Factory>())
            {
                factory.MakeWindowAssociation(form.Handle, WindowAssociationFlags.IgnoreAll);
            }

            SetRenderTarget(device, swapChain, form);

            return new DXWindow(form, device, swapChain);
        }
    }
}